
#include "dtmf.h" 
#include "OS.h" 
#include "App.h" 
#include "debug.h" 
#include "utils.h" 
#include "SysClock.h" 
#include "string.h" 
#include "menu_msg.h" 
#include "storage.h" 
#include "macros.h" 

/* ============================================================================= */
enter_number_t util_enter_number_inf;
/* result: string into enter_passw_inf.inp_buf */
void util_enter_number ( void )
{
	if ( APP.Signal == APP_SIGNAL_CLOSE )
	{
		DBG("\n%s; APP_SIGNAL_CLOSE\n",__FUNCTION__); 
		APP.State = ASTE_FINISH;
	}

	switch( APP.State )
	{
		case ASTE_START:
			DBG("\n%s: started;\n",__FUNCTION__); 
			OS_SET_TIMER(10000, ASTE_FINISH);
			APP.Result = AR_FAIL;
			MemSet((unsigned char*)util_enter_number_inf.inp_buf, UTIL_ENTER_NUMBER_BUF_MAX, 0);
			util_enter_number_inf.p_inp_buf = 0;
			APP.State = ASTE_WAIT_FOR_DIGIT;
			break;

		case ASTE_WAIT_FOR_DIGIT:
			if ( dtmf_digit == DTMF_EMPTY )
				return;

			OS_SET_TIMER(10000, ASTE_FINISH);

			am_stop_msg();

			if ( dtmf_digit == DTMF_POUND )
			{
				util_enter_number_inf.inp_buf[util_enter_number_inf.p_inp_buf] = 0; /* end of string */
				APP.Result = AR_SUCCSESS;
				APP.State = ASTE_FINISH;
			}
			else
			{
				if ( util_enter_number_inf.p_inp_buf < UTIL_ENTER_NUMBER_BUF_MAX )
				{
					util_enter_number_inf.inp_buf[util_enter_number_inf.p_inp_buf++] = dtmf_digit +'0';
				}
			}
			break;

		case ASTE_FINISH:
			OS_Return();
			
	}  
}

/* ============================================================================= */

util_enter_passw_t enter_passw_inf;

void util_enter_password ( void )
{
	switch(APP.State)
	{
		case ASTE_START:
			DBG("\n%s; started;\n",__FUNCTION__); 
			if ( enter_passw_inf.try_num == 0 )
				enter_passw_inf.try_num = 3;


		case ASTE_RESET:
			MemSet(enter_passw_inf.inp_buf, UEP_INP_BUF_SIZE, 0);
			enter_passw_inf.p_inp_buf = 0;
			OS_SET_TIMER(15000,ASTE_RESULT);
			APP.Result = AR_FAIL;
			APP.State = ASTE_IDLE;
			
			if ( OS_Find_App(msgs_play) == APP_NOT_FOUND )
			{
				am_play_msg(AMSG_ENTER_PASSWORD);
			}

			break;

		case ASTE_IDLE:
		
			if ( dtmf_digit != DTMF_EMPTY)
			{
				OS_Send_Signal(msgs_play, APP_SIGNAL_CLOSE);
				am_stop_msg();

						
				if ( dtmf_digit == DTMF_POUND ) /* # */
				{					

					enter_passw_inf.inp_buf[enter_passw_inf.p_inp_buf++] = 0;   /* end of string */

					DBG("\n%s; p_inp_buf: %d\n",__FUNCTION__, enter_passw_inf.p_inp_buf); 
					DBG("\n%s; inp_buf: %s\n",__FUNCTION__, enter_passw_inf.inp_buf); 

					if ( storage.comp(enter_passw_inf.addr_passw, enter_passw_inf.inp_buf, enter_passw_inf.p_inp_buf) 
						  == enter_passw_inf.p_inp_buf )
					{
						APP.Result = AR_SUCCSESS;
					}
					else					  /* wrong password */
					{
						APP.Result = AR_FAIL;
					}
					
					APP.State = ASTE_RESULT;
				}
				else
				{
					if ( enter_passw_inf.p_inp_buf < (UEP_INP_BUF_SIZE-1) )
					{
						enter_passw_inf.inp_buf[enter_passw_inf.p_inp_buf++] = dtmf_digit + '0';
					}
					else
					{
						APP.Result = AR_FAIL;
						APP.State = ASTE_RESULT;
					}
					
				}
			}
			break;

		case 	ASTE_RESULT:
			DBG("\n%s; APP.Result: %x\n",__FUNCTION__,APP.Result); 

			if ( APP.Result == AR_SUCCSESS )
			{
				msg_play__msg = AMSG_PASSWORD_OK;
				APP.State = ASTE_FINISH;
			}
			else
			{
				msg_play__msg = AMSG_PASSWORD_WRONG;

				if ( enter_passw_inf.try_num > 0 )
				{
					enter_passw_inf.try_num--;
					APP.State = ASTE_RESET;
				}
				else
				{
					APP.State = ASTE_FINISH;
				}
			}

			OS_Call_App(0,msg_play);
			break;

		case ASTE_FINISH:
			DBG("\n%s; FINISH\n",__FUNCTION__); 
			OS_Return();
	}
}

/* ============================================================================= */


util_enter_data_t util_enter_data_inf = {NULL, AMSG_END_OF_MSG,AMSG_END_OF_MSG,NULL};

void util_enter_data ( void )
{
	if ( APP.Signal == APP_SIGNAL_CLOSE )
	{
		DBG("\n%s; APP_SIGNAL_CLOSE\n",__FUNCTION__); 
		APP.State = ASTE_FINISH;
	}

	switch( APP.State )
	{
		case ASTE_START:
			DBG("\n%s; started\n",__FUNCTION__); 
			if ( util_enter_data_inf.msgs_to_play )
			{
				msg_play__msgs = util_enter_data_inf.msgs_to_play;
				OS_Run_App(msgs_play);
			}
			else
				if ( util_enter_data_inf.msg_to_play != AMSG_END_OF_MSG )
				{
					am_play_msg(util_enter_data_inf.msg_to_play);
				}

			APP.State = ASTE_PLAY_MSG;
			OS_Call_App(0,util_enter_number);
			break;

		case ASTE_PLAY_MSG:
			APP.State = ASTE_FINISH;
			if ( util_enter_data_inf.post_msg_to_play != AMSG_END_OF_MSG)
			{
				msg_play__msg = util_enter_data_inf.post_msg_to_play;
				OS_Call_App(0,msg_play);
			}	
			break;
				
		case ASTE_FINISH:
			msg_play__msgs = NULL;
			am_stop_msg();
			OS_Kill_App(msgs_play);
			util_enter_data_inf.data = util_enter_number_inf.inp_buf;
			util_enter_data_inf.msg_to_play = AMSG_END_OF_MSG;
			util_enter_data_inf.msgs_to_play = NULL;
			util_enter_data_inf.post_msg_to_play = AMSG_END_OF_MSG;
			APP.Result = AR_SUCCSESS;
			DBG("\n%s; FINISH\n",__FUNCTION__); 
			OS_Return();
			
	}
}

util_audio_record_t	util_audio_record_inf = {AMSG_END_OF_MSG,AMSG_END_OF_MSG,NULL,AMSG_END_OF_MSG};

void util_audio_record ( void )
{
	if ( APP.Signal == APP_SIGNAL_CLOSE )
	{
		DBG("\n%s; APP_SIGNAL_CLOSE\n",__FUNCTION__); 
		APP.State = ASTE_FINISH;
	}

	switch( APP.State )
	{
		case ASTE_START:
			DBG("\n%s; started\n",__FUNCTION__); 
			APP.State = ASTE_AUDIO_RECORD;
			if ( util_audio_record_inf.msgs_to_play  ) 
			{
				msg_play__msgs = util_audio_record_inf.msgs_to_play;
				OS_Call_App(0,msgs_play);
			}
			else
				if ( util_audio_record_inf.msg_to_play != AMSG_END_OF_MSG ) 
				{
					msg_play__msg = util_audio_record_inf.msg_to_play;
					OS_Call_App(0,msg_play);
				}
				else
				{
					APP.Result = AR_FAIL;
					APP.State = ASTE_FINISH;
				}
			break;

		case ASTE_AUDIO_RECORD: 
			am_record_msg(util_audio_record_inf.record_address);
			DBG("\n%s; START RECORD: 0x%X\n",__FUNCTION__,util_audio_record_inf.record_address); 
			APP.State = ASTE_END_OF_AUDIO_RECORD;
			OS_Sleep(3000);
			break;

		case ASTE_END_OF_AUDIO_RECORD: 
			am_stop_msg();
			DBG("\n%s; FINISH RECORD\n",__FUNCTION__); 
			APP.Result = AR_SUCCSESS;
			APP.State = ASTE_FINISH;
			OS_Sleep(100);
			break;

		case ASTE_FINISH:
			util_audio_record_inf.msgs_to_play = NULL;
			util_audio_record_inf.msg_to_play = AMSG_END_OF_MSG;
			util_audio_record_inf.record_address = AMSG_END_OF_MSG;
			DBG("\n%s; APP.Result: 0x%X\n",__FUNCTION__, APP.Result); 
			DBG("\n%s; FINISH\n",__FUNCTION__); 
			OS_Return();
	}
}
